using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Roles;
using Framework.Library.Components.Security;
using Framework.Library.Components.Users;
using Framework.Library.Utilities;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Users
{
	public partial class UserProfile : AuthenticatedControl
	{
		private int userId;
		protected void Page_Load(object sender, EventArgs e)
		{
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
			userId = ConvertUtility.ToInt32(Request.QueryString["uid"]);
            TimeZones.DataSource = StaticData.TimeZones();
            TimeZones.DataBind();
            lblCommandName.Text = AppEnv.CommandName();
            if (CurrentUser.IsSuperAdmin)
            {
                pnlSuperadmin.Visible = true;
            }
			if (!Page.IsPostBack)
			{
                PortalController.LoadPortalsForUserID(lstPortal.Items, CurrentUser.UserID);
			    
				UserInfo user = UserController.GetUser(userId);
				if (user == null)
				{
					this.btnUpdate.Visible = false;
					this.btnManagePassword.Visible = false;
					return;
				}

				lblUsername.Text = user.Username;
                txtName.Text = user.Name;
                txtDisplayName.Text = user.DisplayName;
				txtAddress.Text = user.Address;
				txtEmail.Text = user.Email;
                txtTel.Text = user.Tel;
                txtMobile.Text = user.Mobile;
				chkIsSuperAdmin.Checked = user.IsSuperAdmin;
				txtMobile.Text = user.Mobile;
                lblJoined.Text = user.Joined.ToShortDateString();
                lblLastVisited.Text = user.LastVisit.ToShortDateString();
                TimeZones.SelectedValue = user.TimeZone.ToString();
                PMNotification.Checked = user.PMNotification;				
                
                DataTable dtUserPortals = PortalController.GetPortals(user.UserID);
                foreach (DataRow row in dtUserPortals.Rows)
				{
					if (lstPortal.Items.FindByValue(row["PortalID"].ToString()) != null)
					{
					    lstPortal.Items.FindByValue(row["PortalID"].ToString()).Selected = true;
					}
				}
			}
		}

		protected void btnUpdate_Click(object sender, EventArgs e)
		{
			if (userId == 0) return;
			try
			{ 
                UserInfo user = new UserInfo();
                user.UserID = userId;
                user.Name = txtName.Text;
                user.DisplayName = txtDisplayName.Text.Trim();
                user.Email = txtEmail.Text;
                user.Tel = txtTel.Text.Trim();
                user.Mobile = txtMobile.Text.Trim();
                user.Address = txtAddress.Text.Trim();
                user.LastVisit = DateTime.Now;
                user.IP = HttpContext.Current.Request.UserHostAddress;
                user.TimeZone = Convert.ToInt32(TimeZones.SelectedValue);                
                user.PMNotification = PMNotification.Checked;

                if (pnlSuperadmin.Visible)
                    user.IsSuperAdmin = chkIsSuperAdmin.Checked;
                else
                    user.IsSuperAdmin = false;
			    
				UserController.UpdateUser(user);

                foreach (ListItem item in lstPortal.Items)
				{
					if (item.Selected)
					{
					    RoleController.UpdateUserToRole(user.UserID, AppEnv.DEFAULT_ROLE,Convert.ToInt32(item.Value));
					}
                    else
					{
                        RoleController.RemoveUserFromRole(user.UserID, AppEnv.DEFAULT_ROLE,Convert.ToInt32(item.Value));
					}
				}
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
		}
		protected void btnManagePassword_Click(object sender, EventArgs e)
		{
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.UserPassword)) + "&uid=" + userId);
		}
		protected void btnCancel_Click(object sender, EventArgs e)
		{
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.UserManage)));
		}
	}
}